<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Verify;
use Org\Yike\Rights;
class LoginController extends Controller {
    private $img = '';
    public function  _initialize(){
        $config = array('imageH'=>50,'imageW'=>150,'length'=>4,'fontSize'=>21);
        $this->img = new Verify($config);
    }
    public function index() {
        if (IS_POST) {
            $username = I('post.username');
            $password = I('post.password');
            $verify_code = I('post.verify_code');

            if(!$this->img->check($verify_code,'web')){
                $this->error(L('verify_code_error'));
            }

            $admin = M('admin')->where(array('username'=>$username, 'status'=>1))->find();
            if (!$admin) {
                $this->error(L('admin_not_exist'));
            }
            if ($admin['password'] != md5($password)) {
                $this->error(L('password_error'));
            }

            M('admin')->where(array('id'=>$admin['id']))->save(array('last_time'=>time(), 'last_ip'=>get_client_ip()));

            //session(C('USER_AUTH_KEY'),$admin['id']);
            session('username',$admin['username']);
            session('last_time',date('Y-m-d H:i:s',$admin['last_time']));
            session('last_ip',$admin['last_ip']);
            Rights::getmanagemenu(array('id'=>$admin['id'],'roleid'=>$admin['role_id']));
            //超级管理员识别
           // if($admin['username']==C('RBAC_SUPERADMIN')){
           //     session(C('ADMIN_AUTH_KEY'),true);
            //}
            //给登录用户赋予权限
           // import('ORG.Util.RBAC');
            //RBAC::saveAccessList();
            //p($_SESSION);
            //die;

            $this->success(L('login_success'), U('Index/index'));
        } else {
            $this->display();
        }
    }
    public function logout() {
        // session(C('USER_AUTH_KEY'), null);
        session_unset();
        session_destroy();
        $this->success(L('logout_success'), U('Login/index'));
        exit;
    }

    public function code() {
        $this->img->entry('web');
      // Image::buildImageVerify(1,1,'gif','50','24');
    }
}